package com.next

class Opportunity
{
    String serialNumber
    //订单号

    Address address

    Date createdDate = new Date()
    Date modifiedDate = new Date()

    String status = "Pending"

    String message
    // 买家留言

    OpportunityStage stage

    Integer quantity = 0
    Double unitPrice = 0
    Double totalPrice = 0

    String size

    Date arrivalDate

    static belongsTo = [contact: Contact, product: Product]

    static constraints = {

        product nullable: false, blank: false
        contact nullable: false, blank: false

        serialNumber nullable: true, blank: true
        address nullable: true, blank: true
        message nullable: true, blank: true

        status inList: ["Pending", "Failed", "Completed"]
        status nullable: true, blank: true

        stage nullable: true, blank: true
        quantity nullable: true, blank: true
        unitPrice nullable: true, blank: true
        totalPrice nullable: true, blank: true
        
        arrivalDate nullable: true, blank: true
        size nullable: true, blank: true
        
    }

    def beforeInsert()
    {
        // String charset = (('A'..'Z') + ('0'..'9')).join()
        // String s1 = org.apache.commons.lang.RandomStringUtils.random(3, charset)
        // String s2 = org.apache.commons.lang.RandomStringUtils.random(3, charset)
        // String s3 = org.apache.commons.lang.RandomStringUtils.random(3, charset)
        // serialNumber = "${s1}-${s2}-${s3}"

        Date date = new Date()
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss")
        serialNumber = sdf.format(date)

        totalPrice = quantity * unitPrice

        if (!stage) 
        {
            stage = OpportunityStage.findByName("未发货")
        }
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        totalPrice = quantity * unitPrice
    }
}
